<template>
  <div v-show="isVisible" class="content-api--container" id="plasmo-news-container">
    <div class="news-control-panel">
      <div class="control-header">
        <h3>📰 新闻获取器示例</h3>
        <button class="close-btn" @click="closePanel">×</button>
      </div>
      <div class="control-body">
        <label for="content-num">新闻数量 (1-20):</label>
        <input 
          type="number" 
          id="content-num" 
          min="1" 
          max="20" 
          v-model.number="contentNum"
          @input="validateInput"
        >
        <button 
          @click="handleFetchNews" 
          :disabled="isLoading"
          class="fetch-btn"
        >
          {{ isLoading ? '获取中...' : '获取新闻' }}
        </button>
      </div>
      <div class="news-content">
        <!-- 占位符状态 -->
        <p v-if="!newsData.length && !errorMessage && !isLoading" class="placeholder">
          点击"获取新闻"按钮开始 🚀
        </p>
        
        <!-- 加载状态 -->
        <p v-if="isLoading" class="loading">
          🔄 正在获取新闻数据...
        </p>
        
        <!-- 错误状态 -->
        <p v-if="errorMessage" class="error">
          ❌ {{ errorMessage }}
        </p>
        
        <!-- 新闻列表 -->
        <div v-if="newsData.length > 0 && !isLoading">
          <div 
            v-for="(item, index) in newsData" 
            :key="item.id" 
            class="news-item"
          >
            <h4>{{ index + 1 }}. {{ item.title }}</h4>
            <p>{{ item.body }}</p>
            <small>ID: {{ item.id }} | 用户: {{ item.userId }}</small>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<style>
.content-api--container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 350px;
  max-height: 500px;
  background: #ffffff;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
  z-index: 10000;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  overflow: hidden;
}

.news-control-panel {
  display: flex;
  flex-direction: column;
}

.control-header {
  background: #2196F3;
  color: white;
  padding: 12px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.control-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  color: white;
  font-size: 20px;
  cursor: pointer;
  padding: 0;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-btn:hover {
  background: rgba(255,255,255,0.2);
}

.control-body {
  padding: 16px;
  border-bottom: 1px solid #e0e0e0;
}

.control-body label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #333;
  font-size: 14px;
}

.control-body input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  margin-bottom: 12px;
  box-sizing: border-box;
}

.control-body button {
  width: 100%;
  padding: 10px;
  background: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s;
}

.control-body button:hover:not(:disabled) {
  background: #45a049;
}

.control-body button:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.news-content {
  max-height: 300px;
  overflow-y: auto;
  padding: 16px;
}

.placeholder {
  text-align: center;
  color: #666;
  font-style: italic;
  margin: 0;
}

.loading {
  text-align: center;
  color: #2196F3;
  margin: 0;
}

.error {
  text-align: center;
  color: #f44336;
  margin: 0;
}

.news-item {
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 12px;
  margin-bottom: 12px;
}

.news-item:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

.news-item h4 {
  margin: 0 0 8px 0;
  font-size: 14px;
  font-weight: 600;
  color: #333;
  line-height: 1.4;
}

.news-item p {
  margin: 0 0 8px 0;
  font-size: 13px;
  color: #666;
  line-height: 1.4;
}

.news-item small {
  font-size: 11px;
  color: #999;
}
</style>


<script lang="ts">
import type { PlasmoCSConfig } from "plasmo"

export const config: PlasmoCSConfig = {
  matches: ["<all_urls>"],
  all_frames: true,
  run_at: "document_idle"
}
</script>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { sendToBackground } from "@plasmohq/messaging"

console.log("📰 Plasmo Vue新闻获取器已加载 -", window.location.href)

// 响应式数据
const contentNum = ref(5)
const isLoading = ref(false)
const newsData = ref<any[]>([])
const errorMessage = ref("")
const isVisible = ref(true)

/**
 * 验证输入范围
 */
function validateInput(): void {
  if (contentNum.value < 1) contentNum.value = 1
  if (contentNum.value > 20) contentNum.value = 20
}

/**
 * 获取新闻数据
 */
async function fetchNews(): Promise<void> {
  if (isLoading.value) return
  
  isLoading.value = true
  errorMessage.value = ""
  newsData.value = []
  
  try {
    console.log(`🔄 请求 ${contentNum.value} 条新闻...`)
    
    const response = await sendToBackground({
      name: "fetchNews",
      body: {
        count: contentNum.value
      }
    })
    
    console.log("📰 收到background响应:", response)
    
    if (response.success && response.data) {
      newsData.value = response.data
      console.log(`✅ 成功获取 ${response.data.length} 条新闻`)
    } else {
      errorMessage.value = response.error || "获取新闻失败"
      console.error("❌ 获取新闻失败:", response.error)
    }
    
  } catch (error) {
    errorMessage.value = "网络请求失败"
    console.error("❌ 请求异常:", error)
  } finally {
    isLoading.value = false
  }
}

/**
 * 处理获取新闻按钮点击
 */
async function handleFetchNews(): Promise<void> {
  await fetchNews()
}

/**
 * 关闭面板 - 使用Vue响应式方式
 */
function closePanel(): void {
  isVisible.value = false
}

/**
 * 显示面板 - 使用Vue响应式方式
 */
function showPanel(): void {
  isVisible.value = true
}

// Vue组件挂载后的初始化
onMounted(() => {
  console.log("✅ Vue组件已挂载到页面")
})
</script>
